# Run preamble
source("code/preamble.R")

# Load data
study1_data <- readRDS("data/study1_data.rds")
study2_data <- readRDS("data/study2_data.rds")
study4_data <- readRDS("data/study4_data.rds")
study5_data <- readRDS("data/study5_data.rds")

# Subset Study 2 data
study2_data <- study2_data %>%
    subset(condition != "dist_incen")

# Subset and clean Study 4 data
study4_data <- study4_data %>%
    subset( # Drop Independents and participants in the placebo conditions
        !is.na(pid2) & condition != "Blue vs. Pink" &
            condition != "Red Sox vs. Yankees"
    ) %>%
    mutate( # Create policy variable and update condition variable
        policy = condition %>%
            as.character() %>%
            factor(
                levels = c(
                    "Gun Control",
                    "Border Control",
                    "Abortion Access"
                )
            ),
        condition = "dist_no_correct"
    )

# Subset Study 5 data
study5_data <- study5_data %>%
    subset(
        grepl("^dist_", condition)
    )

# Rename order variable in Study 5 data
study5_data <- study5_data %>%
    rename(
        out_order = per_out_dist_order
    )

# Bind data from different studies
bound_data <- bind_rows(
    study2_data, study4_data, study5_data
) %>%
    select(
        policy, dem_mean, rep_mean, dem_point, rep_point, condition, study, par_pos,
        in_order, out_order, pid2
    )

# Generate ID
bound_data$id <- 1:nrow(bound_data) %>% as.factor()

# Clean up variables
bound_data <- bound_data %>%
    mutate(
        condition = case_when(
            grepl("point_|mean_", condition) ~ "Point Estimate",
            grepl("dist_", condition) ~ "Distribution",
            TRUE ~ "NA"
        )
    ) %>%
    rename(
        `Policy Issue` = policy,
        Study = study
    )

# Compile mean and point variables
bound_data$dem_mean[bound_data$condition == "Point Estimate"] <-
    bound_data$dem_point[bound_data$condition == "Point Estimate"]

bound_data$rep_mean[bound_data$condition == "Point Estimate"] <-
    bound_data$rep_point[bound_data$condition == "Point Estimate"]

# Drop Point Estimate variables
bound_data <- bound_data %>%
    select(-dem_point, -rep_point)

####################################################
### PLOT PERCEIVED POLARIZATION BY QUESTION TYPE ###
####################################################

# Summarize data
per_pol_data <- bound_data %>%
    subset(Study == "Study 2") %>%
    group_by(condition, `Policy Issue`) %>%
    summarize(
        n = n(),
        se = sd(abs(dem_mean - rep_mean), na.rm = TRUE) / sqrt(n),
        polar = mean(abs(dem_mean - rep_mean), na.rm = TRUE)
    )

# Grab actual means
act_pol_data <- study1_data %>%
    select(pid2, policy, par_pos) %>%
    group_by(pid2, policy) %>%
    summarise(
        Actual = mean(par_pos)
    ) %>%
    pivot_wider(
        names_from = pid2,
        values_from = Actual
    ) %>%
    mutate(
        polar = abs(Democrat - Republican)
    ) %>%
    select(policy, polar) %>%
    rename(`Policy Issue` = policy)

# Plot
per_pol_plot <- ggplot() +
    geom_jitter(
        data = bound_data %>%
            subset(Study == "Study 2"),
        aes(
            x = abs(dem_mean - rep_mean), y = condition,
            shape = `Policy Issue`, color = `Policy Issue`
        ),
        alpha = .2
    ) +
    geom_pointrange(
        data = per_pol_data,
        aes(
            x = polar, xmin = polar - (1.96 * se), xmax = polar + (1.96 * se),
            y = condition,
            shape = `Policy Issue`, color = `Policy Issue`
        ),
        position = position_dodge(.4)
    ) +
    geom_vline(
        data = act_pol_data,
        aes(xintercept = polar, color = `Policy Issue`),
        show.legend = FALSE
    ) +
    labs(
        x = 'Perceived Distance Between "Average" Democrat and Republican\n',
        y = "Question Type\n"
    ) +
    scale_color_brewer(palette = "Dark2") +
    theme_bw() +
    theme(
        legend.justification = c(1, 0),
        legend.position = c(1, 0),
        legend.title = element_text(size = 10),
        legend.text = element_text(size = 8),
        legend.background = element_rect(fill = "transparent"),
        axis.text = element_text(size = 10, color = "black"),
        axis.title = element_text(size = 12)
    )

#####################################
### PLOT TOKEN EXTREMITY BY ORDER ###
####################################

# Generate in-party order data
in_order_data <- bound_data %>%
    select(id, Study, `Policy Issue`, pid2, in_order) %>%
    na.omit() %>%
    separate_longer_delim(
        cols = in_order,
        delim = ","
    ) %>%
    separate_wider_delim(
        in_order,
        delim = "-",
        names = c("action", "token_value")
    ) %>%
    group_by(id) %>%
    mutate(order = 1:n()) %>%
    ungroup() %>%
    mutate(
        token_extrem = case_when(
            pid2 == "Democrat" ~ 10 - as.numeric(token_value),
            pid2 == "Republican" ~ as.numeric(token_value)
        )
    ) %>%
    mutate(
        Party = "Perceived Distribution of In-Party"
    )

# Generate out-party order data
out_order_data <- bound_data %>%
    select(id, Study, `Policy Issue`, pid2, out_order) %>%
    na.omit() %>%
    separate_longer_delim(
        cols = out_order,
        delim = ","
    ) %>%
    separate_wider_delim(
        out_order,
        delim = "-",
        names = c("action", "token_value")
    ) %>%
    group_by(id) %>%
    mutate(order = 1:n()) %>%
    ungroup() %>%
    mutate(
        token_extrem = case_when(
            pid2 == "Republican" ~ 10 - as.numeric(token_value),
            pid2 == "Democrat" ~ as.numeric(token_value)
        )
    ) %>%
    mutate(
        Party = "Perceived Distribution of Out-Party"
    )

# Combine order data
order_data <- bind_rows(in_order_data, out_order_data)

# Clean up action variable
order_data <- order_data %>%
    mutate(
        action = case_when(
            grepl("ADD", action) ~ "Token Added",
            grepl("DEL", action) ~ "Token Deleted"
        )
    ) %>%
    rename(Action = action)

# Track number of tokens deleted
order_data <- order_data %>%
    group_by(id, Party) %>%
    mutate(tokens_deleted = cumsum(Action == "Token Deleted"))

# Plot
token_order_plot <- order_data %>%
    subset(Action == "Token Added" & tokens_deleted == 0) %>%
    ggplot(aes(x = order, y = token_extrem)) +
    geom_smooth(
        method = "gam",
        color = "black"
    ) +
    facet_wrap(~Party) +
    theme_bw() +
    labs(
        x = "Time (When Token was Placed)",
        y = "Extremity of Token (in Direction of Party Stereotype)"
    )

##############
### FIGURE ###
##############

# Arrange figure
ggarrange(
    per_pol_plot, token_order_plot,
    nrow = 2, labels = c("A", "B"), heights = c(2, 3)
) %>%
    suppressMessages()

# Save figure
ggsave(
    filename = "figures/question_differences.png",
    device = "png",
    width = 9,
    height = 8
)

# Print disclaimer
cat(
    "\n\n",
    " See question_differences.png for Figure 5."
)

############################################
### TABLES FOR SUPPLEMENTARY INFORMATION ###
############################################

# Generate table describing differences in conditions
act_pol_data %>%
    rename(Reality = polar) %>%
    right_join(per_pol_data, by = "Policy Issue", multiple = "all") %>%
    select(`Policy Issue`, condition, polar, se, n, Reality) %>%
    rename(
        `Perceived Distance Between "Average" Democrat and Republican` = polar,
        `Question Type` = condition,
        SE = se
    ) %>%
    datasummary_df(
        title = "Estimates of Perceived Polarization by Question Type",
        notes = "This table includes data from Study 2.",
        output = "tables/question_differences.txt"
    )

## Print disclaimer
cat(
    "\n\n",
    " See question_differences.txt for data underlying Figure 5 (i.e., Supplementary Table 10)."
)
